﻿using System.Collections.Generic;

namespace OpenWaves.Web
{
#if NET_40_OR_GREATER
    public interface ITreeNode<out T> where T : ITreeNode<T>
#else
    public interface ITreeNode<T> where T : ITreeNode<T>
#endif
    {
        IEnumerable<T> Children { get; }
    }

    public static class TreeNodeExtensions
    {
        public static IEnumerable<T> GetChildrenRecursively<T>(this ITreeNode<T> node) where T : ITreeNode<T>
        {
            foreach(var child in node.Children)
            {
                yield return child;
            }

            foreach(var child in node.Children)
            {
                foreach (var descendant in child.GetChildrenRecursively())
                {
                    yield return descendant;
                }
            }
        }
    }    
}